// components/order/list.js
import {
  submit,
  query
} from '../../api/payment';
import {
  cancel,
  reminder,
  find,
  received,
  payment,
  del,
  paymentFirst
} from '../../api/order.js';
import {
  cancel as cancelAskfor,
  find as findAskfor
} from '../../api/askfor';
const util = require('../../utils/util.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    orderData: {
      type: Object,
      value: null,
      sn:'',
    },
    currentTab:{
      type:Number,
      value:0
    }
  },
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      this.data.orderData.subPrice = this.data.orderData.subPrice.toFixed(2)
      this.setData({
        orderData: this.data.orderData
      })
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    shopId:''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    toShop(){
      wx.navigateTo({
        url: `/pages/shop/shopDetail/index?shopId=${this.data.orderData.shopId}`,
      })
},
    paymentClick() { // 付款
      var _this = this
      wx.showLoading({
        title: '加载中...',
        mask:true
      })
      if(_this.data.orderData.orderType == 1){
        paymentFirst(_this.data.orderData.id).then(res => {
          if (res.type == 'success') {
            console.log(res)
            _this.submitClick(res)
          } else {
            wx.hideLoading()
            wx.showToast({
              icon: "none",
              title: '支付失败',
            })
          }
        }).catch(error => {
          wx.hideLoading()
          wx.showToast({
            icon: "none",
            title: error.content,
          })
        })
      }else{
        payment(_this.data.orderData.id).then(res => {
          if (res.type == 'success') {
            console.log(res)
            _this.submitClick(res)
          } else {
            wx.hideLoading()
            wx.showToast({
              icon: "none",
              title: '支付失败',
            })
          }
        }).catch(error => {
          wx.hideLoading()
          wx.showToast({
            icon: "none",
            title: error.content,
          })
        })
      }
    },
    submitClick(data) {
      var _this = this
      _this.data.sn = data.data.sn
      submit({
        paymentPluginId: 'weixinMiniPayPlugin',
        sn: data.data.sn
      }).then(res => {
        console.log(res)
        wx.hideLoading()
        if (res.type == 'success') {
          wx.requestPayment({
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr,
            'package': res.data.package,
            'signType': res.data.signType,
            'paySign': res.data.paySign,
            'success': function (red) {
              setTimeout(function(){
                _this.queryClick()
              }, 100);
            },
            'fail': function (red) {
              console.log(red);
            }
          })
        } else {
          wx.showToast({
                  icon: "none",
            title: '支付失败',
          })
        }
      }).catch(error => {
        wx.hideLoading()
        wx.showToast({
                icon: "none",
          title: error.content,
        })
      })
    },
    queryClick() {
      var _this = this
      query({
        sn: _this.data.sn
      }).then(res => {
        console.log(res)
        if (res.type == 'success') {
          wx.showToast({
            title: '支付成功',
            icon: 'success',
            duration: 1500,
            success: function () {
              setTimeout(function () {
                _this.refresh()
              }, 1500)
            }
          })
        } else {
          _this.refresh()
        }
      }).catch(error => {
        wx.showToast({
                icon: "none",
          title: error.content,
        })
      })
    },
    refresh() {
      this.triggerEvent('refresh')
    },
    orderClick(){
      if(this.data.currentTab != 3){
        wx.navigateTo({
          url: '/pages/order/order/details?id=' + this.data.orderData.id,
        })
      }else{
        wx.navigateTo({
          url: '/pages/order/refund/details?id=' + this.data.orderData.id,
        })
      }

    },
    evaluateClick(){
      wx.navigateTo({
        url: '/pages/order/evaluate/evaluate?id=' + this.data.orderData.id,
      })
    },
    cancelClick(){
      wx.navigateTo({
        url: '/pages/order/refund/index?id=' + this.data.orderData.id,
      })
    },
    // 取消退款
    cancelAskForClick() {
      var _this = this
      wx.showModal({
        title: '提示',
        content: '是否确认取消',
        success(res) {
          if (res.confirm) {
            wx.showLoading({
              title: '加载中',
              mask:true
            })
            cancelAskfor(_this.data.orderData.id).then(
              res => {
                wx.hideLoading()
                if (res.type == 'success') {
                  console.log(res)
                  _this.refresh()
                  wx.showToast({
                    title: res.content,
                  })
                }
              }
            ).catch(error => {
              wx.hideLoading()
              console.log(error)
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    },
    shopPhoneClick(){
      var _this = this
      findAskfor(_this.data.orderData.id).then(res=>{
        console.log(res)
        if(!util.isNull(res.data.shopVo.telephone)){
          wx.makePhoneCall({
            phoneNumber: res.data.shopVo.telephone ,
            fail:function(res){
              console.log(res)
            }
          })
        }else{
          wx.showToast({
            title: '该商家暂无联系方式',
          })
        }
      }).catch(error => {
        wx.showToast({
          title: '该商家暂无联系方式',
        })
      })
    }
  }
})
